package com.tiantianyuegou.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.sina.weibo.sdk.api.share.Base;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.util.TimeUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 退款详情
 */

public class RefundDetailsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView img_title;
    private TextView text_title;

    private ImageView img_hint;
    private TextView text_hint, text_money_num, text_data_num, text_shop_name, text_type_name,
            text_cause_name, text_explain_name, text_data_apply;

    private JSONObject shopList;
    private String toId, tsName;
    private String TAG = "RefundDetailsActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_details);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadDetails();
    }

    @Override
    public void findViewByIds() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    private void init() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title.setText("退款详情");
        img_title.setOnClickListener(this);

        img_hint = (ImageView) findViewById(R.id.img_hint);
        text_hint = (TextView) findViewById(R.id.text_hint);
        text_money_num = (TextView) findViewById(R.id.text_money_num);
        text_data_num = (TextView) findViewById(R.id.text_data_num);
        text_shop_name = (TextView) findViewById(R.id.text_shop_name);
        text_type_name = (TextView) findViewById(R.id.text_type_name);

        text_cause_name = (TextView) findViewById(R.id.text_cause_name);
        text_explain_name = (TextView) findViewById(R.id.text_explain_name);
        text_data_apply = (TextView) findViewById(R.id.text_data_apply);

        try {
            shopList = new JSONObject(getIntent().getStringExtra("shopList"));
            toId = shopList.getString("toId");
            SPHelper.setToId(toId);
            SPHelper.setTSId(shopList.getString("tsId"));
            tsName = shopList.getString("tsName");
            Log.e(TAG, "toId     " + toId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            default:
                break;
        }
    }

    private void loadDetails() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("toId", SPHelper.getToId());
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.GET_REASON_DETAIL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                Log.e("RefundDetailsActivity", "      " + response);
                try {
                    JSONObject object = new JSONObject(response);
                    showMsg(object.getString("message"));
                    if ("100".equals(object.getString("code"))) {
                        text_hint.setText("退款成功");
                        JSONObject list = object.getJSONObject("list");
                        text_money_num.setText(list.getString("trMoney") + "元");
                        String opTime = list.getString("trOptime");
                        if (opTime != null && !"null".equals(opTime)) {
                            text_data_num.setText(new SelfUtils().getDate(list.getString("trOptime")));
                        }
                        text_shop_name.setText(tsName);
                        text_cause_name.setText(list.getString("twName"));
                        if ("1".equals(list.getString("trType"))) {
                            text_type_name.setText("退货退款");
                        } else {
                            text_type_name.setText("仅退货");
                        }
                        text_explain_name.setText(list.getString("trDesp"));
                        text_data_apply.setText(new SelfUtils().getDate(list.getString("trAddtime")));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}